using Godot;
using System;

public partial class Enemy : PathFollow3D
{
    [Export] public double speed = 2.5;
    [Export] public int MaxHealth = 50;
    [Export] public int DeadGold = 15;
    private Node3D playerBase;
    private PlayerBase playerBaseScript;
    private AnimationPlayer animationPlayer;
    private Bank bankCs;

    private int _currentHealth;

    public int CurrentHealth
    {
        get => _currentHealth;
        set
        {
            if (value < CurrentHealth)
            {
                animationPlayer.Play("TakeDamage");
            }

            _currentHealth = value;
            //  销毁自身
            if (value < 1)
            {
                bankCs.CurrGold += DeadGold;
                QueueFree();
            }
        }
    }

    public override void _Ready()
    {
        bankCs = GetTree().GetFirstNodeInGroup("bank") as Bank;
        animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
        CurrentHealth = MaxHealth;
        playerBase = (Node3D)GetTree().GetFirstNodeInGroup("PlayerBase");
        // speed = 13;
    }

    public override void _Process(double delta)
    {
        this.Progress += (float)(delta * speed);
        if (playerBase != null)
        {
            playerBaseScript = playerBase as PlayerBase;
        }

        if (ProgressRatio >= 1.0)
        {
            // 调用Script中的方法
            playerBaseScript?.takeDamage();
            // 停止运行_Process函数
            SetProcess(false);
            // Free();
            QueueFree();
        }
    }
}